﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using DataLayer.DummaDTO;
using Common.DTO;

namespace DataLayer
{
    /// <summary>
    /// TODO: Comment
    /// </summary>
    public class SaveDBInfo
    {                
        /// <summary>
        /// TODO: Comment
        /// </summary>
        /// <param name="namn"></param>
        /// <returns></returns>
        public static Boolean SavePlayerNameNameInfoDB(string namn)
        {
            var game = new GameDbEntities();

            var nySpelare = new PlayerEntity {Name = namn};

            game.AddToPlayer(nySpelare);
            int saveChanges = game.SaveChanges();

            if(saveChanges != 0)
                return true;

            return false;
        }

        /// <summary>
        /// Deletes a PlayerSession by id, if no PlayerSession is left, the GameSession is deleted too.
        /// </summary>
        /// <param name="playerId"></param>
        /// <param name="sessionId"></param>
        /// <returns></returns>
        public static bool DeletePlayerSession(int playerId, int sessionId)
        {
            try
            {
                var sessions = new GameDbEntities();

                foreach (PlayerGameSessionEntity playerSession in sessions.PlayerGameSession)
                {
                    if (playerSession.GameSessionID == sessionId && playerSession.PlayerID == playerId)
                    {
                        sessions.DeleteObject(playerSession);
                    }
                }

                var specificSession = sessions.GameSession.Where(s => s.ID == sessionId).SingleOrDefault();
                if (specificSession.PlayerGameSession.Count.Equals(0))
                {
                    sessions.DeleteObject(specificSession);
                }

                //foreach (SpelaresSpelsessioner playerSession in sessions.SpelaresSpelsessioner)
                //{
                //    if (playerSession.ID == playerId)
                //    {
                //        sessions.DeleteObject(playerSession);
                //    }
                //}
                sessions.SaveChanges();
                return true;
                
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// TODO: Comment
        /// </summary>
        /// <param name="gameSessionId"></param>
        /// <returns></returns>
        public static bool DeleteGameSession(int gameSessionId)
        {
            try
            {                
                var sessions = new GameDbEntities();
                
                //Get the specific session, loop and delete all playersessions because of foreign key constraints
                var session = sessions.GameSession.Where(s => s.ID == gameSessionId).SingleOrDefault();                
                foreach(PlayerGameSessionEntity playerSession in sessions.PlayerGameSession)
                {
                    if (playerSession.GameSessionID == gameSessionId)
                    {                       
                        sessions.PlayerGameSession.DeleteObject(playerSession);
                    }
                }
                sessions.DeleteObject(session);

                sessions.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Create a gamesession, supposed to return the id of the session
        /// </summary>
        /// <param name="gameId"></param>
        /// <param name="rounds"></param>
        /// <returns>id of the session</returns>
        public static int CreateGameSession(int gameId, int rounds)
        {
            try
            {
                var game = new GameDbEntities();

                var gameSession = new GameSessionEntity
                {
                    GameID = gameId,
                    NoOfGameRounds = rounds,
                    DateStart = DateTime.Now,
                    ActiveGame = false,
                };
                game.AddToGameSession(gameSession);
                game.SaveChanges();

                return gameSession.ID;
            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        /// TODO: Comment
        /// </summary>
        /// <param name="gameSessionId"></param>
        /// <param name="playerId"></param>
        /// <returns></returns>
        public static int CreatePlayerSession(int gameSessionId, int playerId)
        {
            try
            {
                var game = new GameDbEntities();

                var spelaresSpelsessioner = new PlayerGameSessionEntity
                {
                    GameSessionID = gameSessionId,
                    PlayerID = playerId,
                };

                game.AddToPlayerGameSession(spelaresSpelsessioner);
                game.SaveChanges();

                return spelaresSpelsessioner.ID;
            }
            catch
            {
                return -1;
            }
        }

        /// <summary>
        /// TODO: Comment
        /// </summary>
        /// <param name="id"></param>
        /// <param name="rounds"></param>
        /// <returns></returns>
        public static bool UpdateGameRounds(int id, int rounds)
        {
            try
            {
                var game = new GameDbEntities();
                GameSessionEntity gameSession = game.GameSession.Where(s => s.ID == id).SingleOrDefault();

                gameSession.NoOfGameRounds = rounds;

                game.SaveChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Adds a new player to an existing gamesession
        /// </summary>
        /// <param name="playerId"></param>
        /// <param name="sessionId"></param>
        /// <returns>bool</returns>
        public static bool AddPlayerSession(int playerId, int sessionId)
        {
            try
            {
                var game = new GameDbEntities();

                var playerSession = new PlayerGameSessionEntity
                {
                    PlayerID = playerId,
                    GameSessionID = sessionId,
                };
                game.AddToPlayerGameSession(playerSession);
                game.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        /// <summary>
        /// TODO: Comment
        /// </summary>
        /// <param name="gameSessionId"></param>
        /// <param name="playerId"></param>
        /// <param name="playerChoiceId"></param>
        /// <returns></returns>
        public static bool MakePlayerChoice(int gameSessionId, int playerId, int playerChoiceId)
        {
            try
            {
                var game = new GameDbEntities();
                //Gets Round for current gameSessionId
                var gameRounds = game.GameRound.Where(x => x.GameSessionID == gameSessionId).ToList();
                //Gets the round with newest date
                var latestRound = gameRounds.OrderByDescending(x => x.Date).FirstOrDefault();

                var playerChoice = new PlayerChoiceEntity
                {
                    PlayerID = playerId,
                    Choice = playerChoiceId,
                    GameRoundID = latestRound.ID
                };
                game.AddToPlayerChoice(playerChoice);
                game.SaveChanges();

                return true;
            }
            catch
            {
                //throw new NotImplementedException();
                return false;
            }
        }

        public static bool SetWinner(int gameSessionId, int playerId)
        {
            var entities = new GameDbEntities();

            var gameSession = entities.GameSession.FirstOrDefault(g => g.ID == gameSessionId);

            if (gameSession.Winner == null)
            {
                gameSession.ActiveGame = false;
                gameSession.Winner = playerId;

                entities.SaveChanges();

                return true;
            }

            return false;
        }
    }
}
